// pages/order/index.js
import {
    hotel,
    store,
    gourmet,
    platform
} from '../../api/apiData'
var pageNum1 = 1; //页码
Page({

    /**
     * 页面的初始数据
     */
    data: {
        active1: 0,
        active2: 0,
        loadMoreText: '加载中...', //底部文字
        list: [],
        show: false,
        tabList: ['全部', '待支付', '已支付', '已入住', '已完成', '已取消', '已关闭', '已退款'],
        tabList1: ['全部', '待支付', '已支付', '已入住', '已完成', '已取消', '已关闭', '已退款'],
        tabList2: ['全部', '待支付', '待发货', '已发货', '已完成', '已关闭', '已退款'],
        tabList3: ['全部', '待支付', '待使用', '已完成', '已关闭', '已退款'],
        tabList4: ['全部', '待支付', '已支付', '已完成', '已关闭', '已退款']
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if (options.active) {
            this.setData({
                active1: parseInt(options.active)
            })
        } else {
            this.getList(1)
        }
    },
    //   取消订单
    toCancel(e) {
        let that = this
        let item = e.currentTarget.dataset.item
        let order_id = item.id
        wx.showModal({
            title: '提示',
            content: '确定要取消订单吗？',
            showCancel: true, //是否显示取消按钮
            cancelText: "否", //默认是“取消”
            confirmText: "是", //默认是“确定”
            success: function (res) {
                if (res.cancel) {
                    //点击取消,默认隐藏弹框

                } else {
                    //点击确定
                    store.order.cancel({
                        order_id: order_id
                    }, res => {
                        if (res.code == 1) {
                            wx.showToast({
                                title: res.msg,
                            })
                            setTimeout(() => {
                                that.onPullDownRefresh()

                            }, 500)
                        } else {
                            wx.showToast({
                                title: res.msg,
                                icon: 'error'
                            })
                        }
                    })
                }
            },
            fail: function (res) {}, //接口调用失败的回调函数
            complete: function (res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
        })
    },
    toCancel2(e) {
        let that = this
        let item = e.currentTarget.dataset.item
        let order_id = item.id
        wx.showModal({
            title: '提示',
            content: '确定要取消订单吗？',
            showCancel: true, //是否显示取消按钮
            cancelText: "否", //默认是“取消”
            confirmText: "是", //默认是“确定”
            success: function (res) {
                if (res.cancel) {
                    //点击取消,默认隐藏弹框

                } else {
                    //点击确定
                    hotel.order.cancel({
                        order_id: order_id
                    }, res => {
                        if (res.code == 1) {
                            wx.showToast({
                                title: res.msg,
                            })
                            setTimeout(() => {
                                that.onPullDownRefresh()

                            }, 500)
                        } else {
                            wx.showToast({
                                title: res.msg,
                                icon: 'error'
                            })
                        }
                    })
                }
            },
            fail: function (res) {}, //接口调用失败的回调函数
            complete: function (res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
        })
    },
    toCancel3(e) {
        let that = this
        let item = e.currentTarget.dataset.item
        let order_id = item.id
        wx.showModal({
            title: '提示',
            content: '确定要取消订单吗？',
            showCancel: true, //是否显示取消按钮
            cancelText: "否", //默认是“取消”
            confirmText: "是", //默认是“确定”
            success: function (res) {
                if (res.cancel) {
                    //点击取消,默认隐藏弹框

                } else {
                    //点击确定
                    gourmet.order.cancel({
                        order_id: order_id
                    }, res => {
                        if (res.code == 1) {
                            wx.showToast({
                                title: res.msg,
                            })
                            setTimeout(() => {
                                that.onPullDownRefresh()

                            }, 500)
                        } else {
                            wx.showToast({
                                title: res.msg,
                                icon: 'error'
                            })
                        }
                    })
                }
            },
            fail: function (res) {}, //接口调用失败的回调函数
            complete: function (res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
        })
    },
    toCancel4(e) {
        let that = this
        let item = e.currentTarget.dataset.item
        let order_id = item.id
        wx.showModal({
            title: '提示',
            content: '确定要取消订单吗？',
            showCancel: true, //是否显示取消按钮
            cancelText: "否", //默认是“取消”
            confirmText: "是", //默认是“确定”
            success: function (res) {
                if (res.cancel) {
                    //点击取消,默认隐藏弹框

                } else {
                    //点击确定
                    platform.guide.orderCancel({
                        order_id: order_id
                    }, res => {
                        if (res.code == 1) {
                            wx.showToast({
                                title: res.msg,
                            })
                            setTimeout(() => {
                                that.onPullDownRefresh()

                            }, 500)
                        } else {
                            wx.showToast({
                                title: res.msg,
                                icon: 'error'
                            })
                        }
                    })
                }
            },
            fail: function (res) {}, //接口调用失败的回调函数
            complete: function (res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
        })
    },
    // 确认收货
    toReceive(e) {
        let that = this
        let item = e.currentTarget.dataset.item
        let order_id = item.id
        wx.showModal({
            title: '提示',
            content: '确定要确认收货吗？',
            showCancel: true, //是否显示取消按钮
            cancelText: "否", //默认是“取消”
            confirmText: "是", //默认是“确定”
            success: function (res) {
                if (res.cancel) {
                    //点击取消,默认隐藏弹框

                } else {
                    //点击确定
                    store.order.receive({
                        order_id: order_id
                    }, res => {
                        if (res.code == 1) {
                            wx.showToast({
                                title: res.msg,
                            })
                            setTimeout(() => {
                                that.onPullDownRefresh()
                            }, 500)
                        } else {
                            wx.showToast({
                                title: res.msg,
                                icon: 'error'
                            })
                        }
                    })
                }
            },
            fail: function (res) {}, //接口调用失败的回调函数
            complete: function (res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
        })
    },
    // 民宿订单
    toPay(e) {
        console.log(e)
        let item = e.currentTarget.dataset.item
        let that = this
        let order_no = item.order_no
        wx.login({
            success(res) {
                var code = res.code
                wx.showLoading({
                    title: "请稍后...",
                    mask: true
                })
                hotel.order.pay({
                    order_no,
                    code
                }, res => {
                    let ret_code = res.code
                    let ret_msg = res.msg
                    if (ret_code != 1) {
                        //   Toast(ret_msg)
                        wx.showToast({
                            title: ret_msg,
                            icon: 'error'
                        })
                        return
                    }
                    let {
                        nonceStr,
                        paySign,
                        signType,
                        timeStamp
                    } = res.data
                    wx.requestPayment({
                        nonceStr,
                        package: res.data.package,
                        paySign,
                        timeStamp,
                        signType,
                        success(res) {
                            wx.showToast({
                                title: '支付成功',
                                icon: 'success',
                                duration: 1500
                            });
                            setTimeout(() => {
                                that.onPullDownRefresh()
                            }, 500)
                        },
                        fail(res) {
                            wx.showToast({
                                title: '支付失败',
                                duration: 1500,
                                icon: 'error'
                            });
                            setTimeout(() => {
                                that.onPullDownRefresh()
                            }, 500)

                        }
                    })
                })
            }
        })
    },
    // 商城订单
    toPay2(e) {
        console.log(e)
        let item = e.currentTarget.dataset.item
        let that = this
        let order_no = item.order_no
        wx.login({
            success(res) {
                var code = res.code
                wx.showLoading({
                    title: "请稍后...",
                    mask: true
                })
                store.order.pay({
                    order_no,
                    code
                }, res => {
                    let ret_code = res.code
                    let ret_msg = res.msg
                    if (ret_code != 1) {
                        //   Toast(ret_msg)
                        wx.showToast({
                            title: ret_msg,
                            icon: 'error'
                        })
                        return
                    }
                    let {
                        nonceStr,
                        paySign,
                        signType,
                        timeStamp
                    } = res.data
                    wx.requestPayment({
                        nonceStr,
                        package: res.data.package,
                        paySign,
                        timeStamp,
                        signType,
                        success(res) {
                            wx.showToast({
                                title: '支付成功',
                                icon: 'success',
                                duration: 1500
                            });
                            setTimeout(() => {
                                that.onPullDownRefresh()
                            }, 500)
                        },
                        fail(res) {
                            wx.showToast({
                                title: '支付失败',
                                duration: 1500,
                                icon: 'error'
                            });
                            setTimeout(() => {
                                that.onPullDownRefresh()
                            }, 500)

                        }
                    })
                })
            }
        })
    },
    // 美食订单
    toPay3(e) {
        console.log(e)
        let item = e.currentTarget.dataset.item
        let that = this
        let order_no = item.order_no
        wx.login({
            success(res) {
                var code = res.code
                wx.showLoading({
                    title: "请稍后...",
                    mask: true
                })
                gourmet.order.pay({
                    order_no,
                    code
                }, res => {
                    let ret_code = res.code
                    let ret_msg = res.msg
                    if (ret_code != 1) {
                        //   Toast(ret_msg)
                        wx.showToast({
                            title: ret_msg,
                            icon: 'error'
                        })
                        return
                    }
                    let {
                        nonceStr,
                        paySign,
                        signType,
                        timeStamp
                    } = res.data
                    wx.requestPayment({
                        nonceStr,
                        package: res.data.package,
                        paySign,
                        timeStamp,
                        signType,
                        success(res) {
                            wx.showToast({
                                title: '支付成功',
                                icon: 'success',
                                duration: 1500
                            });
                            setTimeout(() => {
                                that.onPullDownRefresh()
                            }, 500)
                        },
                        fail(res) {
                            wx.showToast({
                                title: '支付失败',
                                duration: 1500,
                                icon: 'error'
                            });
                            setTimeout(() => {
                                that.onPullDownRefresh()
                            }, 500)

                        }
                    })
                })
            }
        })
    },
    // 导游订单
    toPay4(e) {
        console.log(e)
        let item = e.currentTarget.dataset.item
        let that = this
        let order_no = item.order_no
        wx.login({
            success(res) {
                var code = res.code
                wx.showLoading({
                    title: "请稍后...",
                    mask: true
                })
                platform.guide.orderPay({
                    order_no,
                    code
                }, res => {
                    let ret_code = res.code
                    let ret_msg = res.msg
                    if (ret_code != 1) {
                        //   Toast(ret_msg)
                        wx.showToast({
                            title: ret_msg,
                            icon: 'error'
                        })
                        return
                    }
                    let {
                        nonceStr,
                        paySign,
                        signType,
                        timeStamp
                    } = res.data
                    wx.requestPayment({
                        nonceStr,
                        package: res.data.package,
                        paySign,
                        timeStamp,
                        signType,
                        success(res) {
                            wx.showToast({
                                title: '支付成功',
                                icon: 'success',
                                duration: 1500
                            });
                            setTimeout(() => {
                                that.onPullDownRefresh()
                            }, 500)
                        },
                        fail(res) {
                            wx.showToast({
                                title: '支付失败',
                                duration: 1500,
                                icon: 'error'
                            });
                            setTimeout(() => {
                                that.onPullDownRefresh()
                            }, 500)

                        }
                    })
                })
            }
        })
    },
    // 获取列表
    getList(page) {
        let that = this
        console.log(8888)
        console.log(that.data.active1)
        if (that.data.active1 == 0) {
            hotel.order.myList({
                page: page,
                size: 10,
                status: that.data.active2
            }, res => {
                that.setData({
                    total: res.data.total
                })
                if (res.data.list.length > 0) {
                    res.data.list.forEach(item => {
                        if (item.status == 1) {
                            console.log(item.expire_time)
                            const minutes = that.calculateMinutesBetweenDates(item.expire_time);
                            console.log(minutes); // 输出：30
                            item.minutes = minutes
                        }

                    })
                    that.setData({
                        list: that.data.list.concat(res.data.list) //合并数据
                    })

                    if (11 > res.data.list.length > 0) {
                        that.setData({
                            loadMoreText: '' //没有更多了
                        })
                    }
                } else {
                    if (page !== 1) {
                        that.setData({
                            loadMoreText: '暂无数据'
                        })
                    } else {
                        that.setData({
                            list: [],
                            loadMoreText: '暂无数据'
                        })
                    }
                }
                wx.stopPullDownRefresh();
            })
        } else if (that.data.active1 == 1) {
            store.order.myList({
                page: page,
                size: 10,
                status: that.data.active2
            }, res => {
                that.setData({
                    total: res.data.total
                })
                if (res.data.list.length > 0) {
                    res.data.list.forEach(item => {
                        if (item.status == 1) {
                            console.log(item.expire_time)
                            const minutes = that.calculateMinutesBetweenDates(item.expire_time);
                            console.log(minutes); // 输出：30
                            item.minutes = minutes
                        }

                    })
                    that.setData({
                        list: that.data.list.concat(res.data.list) //合并数据
                    })

                    if (11 > res.data.list.length > 0) {
                        that.setData({
                            loadMoreText: '' //没有更多了
                        })
                    }
                } else {
                    if (page !== 1) {
                        that.setData({
                            loadMoreText: '暂无数据'
                        })
                    } else {
                        that.setData({
                            list: [],
                            loadMoreText: '暂无数据'
                        })
                    }
                }
                wx.stopPullDownRefresh();
            })
        } else if (that.data.active1 == 2) {
            gourmet.order.myList({
                page: page,
                size: 10,
                status: that.data.active2
            }, res => {
                that.setData({
                    total: res.data.total
                })
                if (res.data.list.length > 0) {
                    res.data.list.forEach(item => {
                        if (item.status == 1) {
                            console.log(item.expire_time)
                            const minutes = that.calculateMinutesBetweenDates(item.expire_time);
                            console.log(minutes); // 输出：30
                            item.minutes = minutes
                        }

                    })
                    that.setData({
                        list: that.data.list.concat(res.data.list) //合并数据
                    })

                    if (11 > res.data.list.length > 0) {
                        that.setData({
                            loadMoreText: '' //没有更多了
                        })
                    }
                } else {
                    if (page !== 1) {
                        that.setData({
                            loadMoreText: '暂无数据'
                        })
                    } else {
                        that.setData({
                            list: [],
                            loadMoreText: '暂无数据'
                        })
                    }
                }
                wx.stopPullDownRefresh();
            })
        } else {
            let status = that.data.active2
            if (status == 0) {
                status = ''
            }
            platform.guide.myList({
                page: page,
                size: 10,
                status: status
            }, res => {
                that.setData({
                    total: res.data.total
                })
                if (res.data.list.length > 0) {
                    res.data.list.forEach(item => {
                        if (item.status == 1) {
                            console.log(item.expire_time)
                            const minutes = that.calculateMinutesBetweenDates(item.expire_time);
                            console.log(minutes); // 输出：30
                            item.minutes = minutes
                        }

                    })
                    that.setData({
                        list: that.data.list.concat(res.data.list) //合并数据
                    })

                    if (11 > res.data.list.length > 0) {
                        that.setData({
                            loadMoreText: '' //没有更多了
                        })
                    }
                } else {
                    if (page !== 1) {
                        that.setData({
                            loadMoreText: '暂无数据'
                        })
                    } else {
                        that.setData({
                            list: [],
                            loadMoreText: '暂无数据'
                        })
                    }
                }
                wx.stopPullDownRefresh();
            })
        }

    },
    calculateMinutesBetweenDates(targetDate) {
        const now = new Date();
        const target = new Date(targetDate);

        // 获取两个日期的毫秒数差
        const differenceInMilliseconds = target.getTime() - now.getTime();

        // 将毫秒差转换为分钟
        const differenceInMinutes = Math.floor(differenceInMilliseconds / (1000 * 60));

        return differenceInMilliseconds;
    },

    onChange1(e) {
        this.setData({
            active1: e.detail.index,
        })
        if (e.detail.index == 1) {
            this.setData({
                tabList: this.data.tabList2
            })
        } else if (e.detail.index == 0) {
            this.setData({
                tabList: this.data.tabList1
            })
        } else if (e.detail.index == 2) {
            this.setData({
                tabList: this.data.tabList3
            })
        } else {
            this.setData({
                tabList: this.data.tabList4
            })
        }
        this.onPullDownRefresh()
    },
    onChange2(e) {
        this.setData({
            active2: e.detail.index
        })
        this.onPullDownRefresh()
    },
    finished() {
        this.onPullDownRefresh()

    },
    toCom() {
        wx.navigateTo({
            url: '/pages/order/comment',
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        // pageNum1 = 1
        // this.setData({
        //     list: [],
        // });
        // this.getList(1)

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
        pageNum1 = 1
        this.setData({
            list: [],
        });
        this.getList(1)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        pageNum1 += 1;
        this.getList(pageNum1)
    },


    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})